package cn.yunhe.service;

import cn.yunhe.dao.IDriverDao;
import cn.yunhe.entity.Driver;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;


import java.util.List;

public class IDriverService {


    SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
    IDriverDao driverDao = sqlSession.getMapper(IDriverDao.class);


    public boolean addDriver(Driver driver) {
        boolean flag = driverDao.addDriver(driver);
        try {
            if (flag) {
                sqlSession.commit();
                return true;
            } else {
                return false;
            }
        } finally {
            sqlSession.close();
        }
    }


    /**
     * 所有司机信息
     *
     * @return
     */
    public List<Driver> getAllDriver() {

        try {
            return driverDao.getAllDriver();
        } finally {
            sqlSession.close();
        }
    }


    /**
     * 司机总人数
     *
     * @return
     */
    public int countDriver() {
        try {
            int count = driverDao.countDriver();
            return count;
        } finally {
            sqlSession.close();
        }
    }


    /**
     * 编辑司机信息
     *
     * @return
     */
    public boolean updateDriver(Driver driver) {
        try {
            if (driverDao.updateDriver(driver)) {
                sqlSession.commit();
                return true;
            } else {
                return false;
            }
        } finally {
            sqlSession.close();
        }
    }


    /**
     * 根据指定Id查询司机
     *
     * @param indexId
     * @return
     */
    public Driver getDriverById(int indexId) {

        try {
            Driver driver = driverDao.getDriverById(indexId);
            return driver;
        } finally {
            sqlSession.close();
        }
    }


    /**
     * 删除司机（禁用 将flag状态改为1）
     *
     * @param indexId
     * @return
     */
    public boolean delDriver(int indexId) {
        try {
            if (driverDao.delDriver(indexId)){
                sqlSession.commit();
                return true;
            }else{
                return false;
            }
        }finally {
            sqlSession.close();
        }
    }

    /**
     * 批量删除司机信息
     *
     * @param checkedId
     * @return
     */
    public boolean falseDelter(String checkedId){
        try {
            if (driverDao.falseDelete(checkedId.split(","))){
                sqlSession.commit();
                return true;
            }else{
                return false;
            }
        }finally {
            sqlSession.close();
        }

    }

}
